package app.auto.runner.base.type;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateUtil {
    public static String getAMPM(Object date){

        return getAMPMPrivate(date);
    }

    private static String getAMPMPrivate(Object date) {
        String rs = "";
        String str = "";
        if(date instanceof Date){

            SimpleDateFormat df = new SimpleDateFormat("HH");
            str = df.format(date);
        }else if(date instanceof String){
            if(date.toString().length()==2){
                str = ((String) date);
                if(str.startsWith("0")){
                    str = str.substring(1);
                }
            }else return (String) date;
        }
        int a = Integer.parseInt(str);
        if (a >= 0 && a <= 6) {
            rs = "凌晨";
        }
        if (a > 6 && a <= 12) {
            rs = "上午";
        }
        if (a > 12 && a <= 13) {
            rs = "中午";
        }
        if (a > 13 && a <= 18) {
            rs = "下午";
        }
        if (a > 18 && a <= 24) {
            rs = "晚上";
        }
        return rs;
    }

    public static String dateToWeek(String datetime) throws java.text.ParseException {
        return dateToWeekPrivate(datetime);
    }

    private static String dateToWeekPrivate(String datetime) throws ParseException {
        SimpleDateFormat f = new SimpleDateFormat("yyyy/MM/dd");
        String[] weekDays = { "星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六" };
        Calendar cal = Calendar.getInstance(); // 获得一个日历
        Date datet = null;
        datet = (Date) f.parse(datetime);
        cal.setTime(datet);
        int w = cal.get(Calendar.DAY_OF_WEEK) - 1; // 指示一个星期中的某天。
        if (w < 0)
            w = 0;
        System.out.println(weekDays[w]);//星期二
        return weekDays[w];
    }
}
